<?php

namespace App\Http\Controllers\Marketing;


use App\Model\Marketing\Post;
use App\Model\System\User;
use Illuminate\Http\Request;
use App\Model\System\Token;
use Laravel\Lumen\Routing\Controller as BaseController;

use App\Http\Controllers\MessagePacket;


class PostController extends BaseController
{
    use MessagePacket;

    /**
     * 获取单一
     * @param $id
     * @return packetMessage
     */
    function get($id)
    {
        $id = intval($id);
        $obj = Post::find($id);
        $obj->url = "article.html?id=".$obj->id;
        if(empty($obj)){
            return $this->fail($obj, '数据不存在');
        }
        return $this->success($obj);
    }


    /**
     * 批量查询
     * @param Request $request
     * @return packetMessage
     */
    function gets(Request $request)
    {
        $query =  Post::wherenotNull('id');
        if($request->has('status')){
            $status = $request->get('status');
            $query->where('status', $status);
        }
        $query->where(function ($query) use ($request){
            if($request->has('title')){
                $title = $request->get('title');
                $query->orwhere('title', 'like',  "%$title%");
            }
        });

        $host = $request->getScheme().'://'.$request->getHttpHost();

        # 需要／不需要 分页
        if($request->has('page')) {
            $limit = 30;
            if ($request->has('perPage')) {
                $limit = $request->get('perPage');
            }
            $post = $query->paginate($limit)->toArray();
            $data['list'] = $post['data'];
            unset($post['data']);
            $data['pager'] = $post;
            foreach ($data['list'] as $k => &$item) {
                $item['url'] = $host."/articles/article.html?id=" . $item['id'];
                $user = User::find($item['created_by']);
                $username = $user->account;
                if(empty($username)){
                    $username = $user->number;
                }
                if(empty($username)){
                    $username = $user->email;
                }
                $item['created_by'] = $username;
                $user = User::find($item['updated_by']);
                $username = $user->account;
                if(empty($username)){
                    $username = $user->number;
                }
                if(empty($username)){
                    $username = $user->email;
                }
                $item['updated_by'] = $username;
            }
        }else{
            $data = $query->get()->toArray();
            foreach ($data as $k => &$item) {
                $item['url'] = $host."/articles/article.html?id=" . $item['id'];
                $user = User::find($item['created_by']);
                $username = $user->account;
                if(empty($username)){
                    $username = $user->number;
                }
                if(empty($username)){
                    $username = $user->email;
                }
                $item['created_by'] = $username;
                $user = User::find($item['updated_by']);
                $username = $user->account;
                if(empty($username)){
                    $username = $user->number;
                }
                if(empty($username)){
                    $username = $user->email;
                }
                $item['updated_by'] = $username;

            }
        }

        if(empty($data)){
            return $this->fail($data, '数据不存在');
        }
        return $this->success($data);
    }


    /**
     * 添加一个
     * @param Request $request
     * @return packetMessage
     */
    function  create(Request $request)
    {
        $obj = new Post($request->all());
        $obj->created_by = Token::user()->userid;
        $obj->updated_by = Token::user()->userid;
        if($obj->save()){
            return $this->success($obj);
        }else{
            return $this->fail(false,'保存失败');
        }
    }


    /**
     * 更新某个
     * @param Request $request
     * @return packetMessage
     */
    function  update($id, Request $request)
    {
        $obj = Post::find($id);
        $obj->update($request->all());
        $obj->updated_by = Token::user()->userid;

        if($obj->save()){
            return $this->success($obj);
        }else{
            return $this->fail(false,'保存失败');
        }
    }

    /**
     * 禁用
     * @param $id
     * @return packetMessage
     */
    function  disable($id)
    {
        $obj = Post::find($id);
        $obj->status = 0;
        if($obj->save()){
            return $this->success(true);
        }else{
            return $this->fail(false, '失败');
        }
    }

    /**
     * 禁用
     * @param $id
     * @return packetMessage
     */
    function  publish($id)
    {
        $obj = Post::find($id);
        $obj->status = Post::PUBLISH;
        if($obj->save()){
            return $this->success(true);
        }else{
            return $this->fail(false, '失败');
        }
    }

    /**
     * 禁用
     * @param $id
     * @return packetMessage
     */
    function  unpublish($id)
    {
        $obj = Post::find($id);
        $obj->status = Post::UNPUBLISH;
        if($obj->save()){
            return $this->success(true);
        }else{
            return $this->fail(false, '失败');
        }
    }

    /**
     * 删除
     * @param $id
     * @return packetMessage
     */
    function  delete($id)
    {
        if(Post::destroy($id)){
            return $this->success(true);
        }else{
            return $this->fail(false,'删除失败');
        }

    }

}
